package com.example.demo.controller;

import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;

import java.io.File;
import java.io.IOException;

public class test {
    public static void main(String[] args) throws IOException {
        // create a new PDF document
        PDDocument doc = new PDDocument();
        PDPage page = new PDPage();

// add a new AcroForm and add that to the document
        PDAcroForm form = new PDAcroForm(doc);
        doc.getDocumentCatalog().setAcroForm(form);

// Add and set the resources and default appearance at the form level
        PDFont font = PDType0Font.load(doc, new File("C:\\Windows\\Fonts\\SIMYOU.TTF"));
        PDResources res = new PDResources();
        COSName fontName = res.add(font);
        form.setDefaultResources(res);
        String da = "/" + fontName.getName() + " 0 Tf 0 g";
        form.setDefaultAppearance(da);

// add a page to the document
        doc.addPage(page);

// add a form field to the form
        PDTextField textBox = new PDTextField(form);
        textBox.setPartialName("我");
        form.getFields().add(textBox);

// specify the annotation associated with the field
// and add it to the page
        PDAnnotationWidget widget = textBox.getWidget();
        PDRectangle rect = new PDRectangle(100f,300f,120f,350f);
        widget.setRectangle(rect);
        page.getAnnotations().add(widget);

// set the field value
        textBox.setValue("我");
        doc.save("C:\\Users\\Administrator\\Desktop\\ChineseOut.pdf");
    }
}
